﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.startup {
    import flash.utils.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.components.*;
    import com.adobe.serialization.json.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.fountain.*;
    import net.wooga.pets.services.*;
    import net.wooga.pets.models.kiosk.*;

    public class LoadBasicDataCommand extends Command {

		[Inject]
        public var environmentModel:EnvironmentModel;
		[Inject]
        public var sessionModel:SessionModel;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var fountainModel:FountainModel;
		[Inject]
        public var paymentModel:PaymentModel;
		[Inject]
        public var computerModel:ComputerModel;
		[Inject]
        public var socialNetwork:ISocialNetworkService;
		[Inject]
        public var achievementModel:AchievementModel;
		[Inject]
        public var levelModel:LevelModel;
		[Inject]
        public var protocolModel:ProtocolModel;
		[Inject]
        public var kioskModel:KioskModel;
		[Inject]
        public var contractsModel:ContractsModel;
		[Inject]
        public var collectableModel:CollectableModel;
        private var _startData:Object;
        private var _protocolFile:String = "config/protocol.json"

        override public function execute():void{
            var _local1:RequestEvent = new RequestEvent(EventConstants.SEND_REQUEST, this.onProtocolData);
            _local1.addAssetRequest(this._protocolFile);
            dispatch(_local1);
        }
        private function onProtocolData(_arg1:ResultVO):void{
            var _local2:RequestEvent;
            if (_arg1.success){
                this.protocolModel.protocol = _arg1.data[this._protocolFile];
                _local2 = new RequestEvent(EventConstants.SEND_REQUEST, this.onEnvironmentData);
                _local2.addAssetRequest(ProtocolConstants.ENV, this.environmentModel.environment);
                dispatch(_local2);
            };
        }
        private function onEnvironmentData(_arg1:ResultVO):void{
            var _local2:Object;
            var _local3:String;
            var _local4:Object;
            var _local5:RequestEvent;
            var _local6:String;
            var _local7:String;
            log(_arg1.errorMessage);
            if (_arg1.success){
                this.environmentModel.config = _arg1.data[ProtocolConstants.ENV];
                _local2 = JSON.decode(((contextView.stage.loaderInfo.parameters.gets) || ("{}")));
                for (_local3 in _local2) {
                    this.environmentModel.setConfig(_local3, _local2[_local3]);
                };
                _local4 = {facebook_id:this.userModel.mySocialID, rooms:0};
                if (this.environmentModel.revision){
                    _local4["client_revision"] = this.environmentModel.revision;
                };
                _local5 = new RequestEvent(EventConstants.SEND_REQUEST, this.onStartData);
                _local6 = this.sessionModel.variant.toLowerCase();
                _local7 = this.environmentModel.language;
                _local5.addAssetRequest(ProtocolConstants.PAYMENT, _local6);
                _local5.addAssetRequest(ProtocolConstants.FOUNTAIN, _local6);
                _local5.addAssetRequest(ProtocolConstants.DISEASES, _local6);
                _local5.addAssetRequest(ProtocolConstants.ACHIEVEMENTS, _local6);
                _local5.addAssetRequest(ProtocolConstants.LEVELS, _local6);
                _local5.addAssetRequest(ProtocolConstants.LOCALE, _local7);
                _local5.addAssetRequest(ProtocolConstants.CRAFTING, _local6);
                _local5.addAssetRequest(ProtocolConstants.CONTRACTS, _local6);
                _local5.addAssetRequest(ProtocolConstants.COLLECTIONS, _local6);
                _local5.addAssetRequest(ProtocolConstants.SHOP, _local6);
                _local5.addRequest("start_game", _local4);
                log(_local4["client_revision"]);
                dispatch(_local5);
            };
        }
        private function onStartData(_arg1:ResultVO):void{
            var _local2:Object;
            var _local3:Array;
            var _local4:RequestEvent;
            var _local5:int;
            if (_arg1.success){
                _local2 = _arg1.data;
                this._startData = _local2.start_game;
                this.languageModel.languageCollection = _local2[ProtocolConstants.LOCALE].text_ids;
                this.paymentModel.data = _local2[ProtocolConstants.PAYMENT];
                this.kioskModel.productList = _local2[ProtocolConstants.CRAFTING];
                for each (_local3 in _local2[ProtocolConstants.FOUNTAIN]) {
                    this.fountainModel.addEntry(_local3);
                };
                for each (_local3 in _local2[ProtocolConstants.DISEASES]) {
                    this.computerModel.addEntry(_local3);
                };
                this.parseAchievements(_local2[ProtocolConstants.ACHIEVEMENTS]);
                this.storeLevelMatrix(_local2[ProtocolConstants.LEVELS]);
                this.storeContracts(_local2[ProtocolConstants.CONTRACTS]);
                this.storeCollections(_local2[ProtocolConstants.COLLECTIONS]);
                this.storeShopMatrix(_local2[ProtocolConstants.SHOP]);
                _local4 = new RequestEvent(EventConstants.SEND_REQUEST, this.onBasicAssets, GameConstants.PRELOAD_BASICS);
                _local5 = this._startData.level_info.level;
                this.addAssetRequest(_local4, GameConstants.AVATAR_ASSET);
                this.addAssetRequest(_local4, GameConstants.BACKGROUND_ASSET);
                this.addAssetRequest(_local4, GameConstants.ICONS_ASSETS);
                this.addAssetRequest(_local4, GameConstants.INTERFACE_ASSETS);
                this.addAssetRequest(_local4, GameConstants.ROOMS_ASSETS);
                this.addAssetRequest(_local4, GameConstants.THUMB_ASSETS);
                this.addAssetRequest(_local4, GameConstants.MISSION_ASSETS);
                this.addAssetRequest(_local4, GameConstants.REPRISE_COMPONENTS);
                this.addAssetRequest(_local4, GameConstants.COMPUTER_DISPLAY_ASSETS);
                this.addAssetRequest(_local4, GameConstants.LETTER_ASSET);
                this.addAssetRequest(_local4, GameConstants.LEVEL_UP_ASSETS);
                this.addAssetRequest(_local4, GameConstants.VISIT_FRIEND_ASSETS);
                this.addAssetRequest(_local4, GameConstants.ACHIEVEMENT_ASSETS);
                this.addAssetRequest(_local4, GameConstants.FOUNTAIN_ASSETS, (_local5 >= (GameConstants.MIN_LEVEL_FOUNTAIN - 1)));
                this.addAssetRequest(_local4, GameConstants.GOODS_ASSETS);
                this.addAssetRequest(_local4, GameConstants.BABY_CYCLE);
                this.addAssetRequest(_local4, GameConstants.PANDA_BABY_ASSET);
                if (_local5 > 1){
                };
                dispatch(_local4);
                this.socialNetwork.login(contextView.stage.loaderInfo);
            };
        }
        private function storeShopMatrix(_arg1:Array):void{
            dispatch(new InfoEvent(EventConstants.STORE_SHOP_DATA, null, _arg1));
        }
        private function addAssetRequest(_arg1:RequestEvent, _arg2:String, _arg3:Boolean=true):void{
            if (_arg3){
                _arg1.addAssetRequest(ProtocolConstants.LOAD_ASSET, _arg2);
            };
        }
        private function parseAchievements(_arg1:Array):void{
            var _local2:AchievementVO;
            var _local3:RewardVO;
            var _local4:Object;
            var _local5:Object;
            for each (_local4 in _arg1) {
                _local2 = new AchievementVO(eventDispatcher);
                _local2.id = _local4.id;
                _local2.unlockLevel = _local4.unlock_level;
                for each (_local5 in _local4.medals) {
                    _local3 = new RewardVO();
                    _local2.addReward(_local3);
                    _local3.id = _local5.code;
                    _local3.medal = _local5.medal;
                    _local3.completed = false;
                    _local3.pointsNeeded = _local5.points_needed;
                    _local3.rewardType = _local5.reward_type;
                    _local3.numberOfRewardsYouGet = _local5.number_of_rewards;
                    _local3.xpYouGet = _local5.xp;
                };
                this.achievementModel.addAchievement(_local2);
            };
        }
        private function storeLevelMatrix(_arg1:Array):void{
            var _local2:LevelVO;
            var _local4:Array;
            var _local3 = 1;
            for each (_local4 in _arg1) {
                _local2 = new LevelVO();
                _local2.level = _local3;
                _local2.xp = _local4[0];
                _local2.slots = _local4[1];
                _local2.energy = _local4[2];
                _local2.coins = _local4[3];
                _local2.cash = _local4[4];
                _local2.gift = _local4[5];
                _local2.room = _local4[6];
                _local2.upgrades = _local4[7];
                _local2.titleKey = _local4[8];
                _local2.graphicKey = _local4[9];
                _local2.descriptionKey = _local4[10];
                this.levelModel.addLevel(_local2);
                _local3++;
            };
        }
        private function storeContracts(_arg1:Array):void{
            var _local2:ContractOfferVO;
            var _local3:Object;
            for each (_local3 in _arg1) {
                _local2 = new ContractOfferVO();
                _local2.code = _local3.code;
                _local2.goods = _local3.goods;
                _local2.readyIn = _local3.ready_in;
                _local2.coins = _local3.coins;
                _local2.productId = _local3.product_id;
                _local2.productSize = _local3.product_size;
                this.contractsModel.addContract(_local2);
            };
        }
        private function storeCollections(_arg1:Array):void{
            var _local2:Object;
            var _local3:CollectableVO;
            for each (_local2 in _arg1) {
                _local3 = new CollectableVO();
                _local3.type = _local2.type;
                _local3.requires = _local2.requires;
                _local3.rewardPet = _local2.rewardPet;
                _local3.rewardPetAmount = _local2.rewardPetAmount;
                _local3.rewardOther = _local2.rewardOther;
                _local3.rewardOtherAmount = _local2.rewardOtherAmount;
                this.collectableModel.addTypeToMatrix(_local3);
            };
        }
        private function onBasicAssets(_arg1:ResultVO):void{
            var _local2:MainView;
            var _local3:String;
            if (_arg1.success){
                dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.LOADING, {progress:66, elapsed:getTimer()}));
                TextFormats.registerFonts();
                _local2 = new MainView();
                contextView.addChild(_local2);
                mediatorMap.createMediator(_local2);
                _local3 = ((this.userModel.userName) || (this.languageModel.getTextByKey("global_default_name")));
                this.languageModel.replaceUsernamePlacehholders(_local3);
                this.languageModel.replaceFormatTags();
                dispatch(new InfoEvent(EventConstants.STORE_START_DATA, null, this._startData));
            };
        }

    }
}//package net.wooga.pets.controllers.startup 
